<template>
  <div class="container">
    <div class="box">
      <!-- 头像部分 -->
      <div class="avatar">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单 -->
      <el-form ref="formRef" class="forms" :rules="rules" :model="form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login" plain>登录</el-button>
          <el-button type="info" @click="resetForm" plain>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录表单的数据绑定数据
      form: {
        username: 'admin',
        password: '123456'
      },
      //表单的验证规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //登录之前的校验
    login() {
      this.$refs.formRef.validate(async valid => {
        // console.log(valid);
        if (!valid) return
        const { data: response } = await this.$axios.post('/login', this.form)
        // console.log(response);
        if (response.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        //将登录成功之后的token保存到客户端的sessionStorage中
        window.sessionStorage.setItem('token', response.data.token)
        //通过编程式导航跳转到后台主页
        this.$router.push('/home')
      })
    },
    //重置登录表单
    resetForm() {
      //console.log(this);
      this.$refs.formRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  position: relative;
  background-color: #2b4b6b;
  height: 100%;
}
.box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 0 20px;
}
.avatar {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 5px;
  box-shadow: 0 0 10px #ddd;
  background-color: #fff;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.forms {
  margin-top: 100px;
  outline: none;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
